﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace NetFrameworkExtensions.Mathematics
{
  public class MovingLinearRegressionAccumulator
  {
    int length;
    Queue<Data> queue;
    LinearRegressionAccumulator accum;

    public MovingLinearRegressionAccumulator(int length)
    {
      Contract.Requires(length > 1);
      this.length = length;
      queue = new Queue<Data>();
      accum = new LinearRegressionAccumulator();
    }

    public double M
    {
      get
      {
        return accum.M;
      }
    }
    public double N
    {
      get
      {
        return accum.N;
      }
    }

    public void Add(double x, double y)
    {
      accum.Add(x, y);
      queue.Enqueue(new Data { x = x, y = y });
      while (queue.Count > length)
      {
        var d = queue.Dequeue();
        accum.Remove(d.x, d.y);
      }
    }

    struct Data
    {
      public double x;
      public double y;
    }
  }
}
